package com.apobates.jforum2.troll.actionlog;

import com.apobates.jforum2.troll.actionlog.biz.BoardActionCollectionService;
import com.apobates.jforum2.troll.actionlog.biz.TopicActionCollectionService;
import com.apobates.jforum2.troll.actionlog.entity.BoardActionCollection;
import com.apobates.jforum2.troll.actionlog.entity.TopicActionCollection;
import com.apobates.jforum2.troll.regular.ForumActionEnum;
import com.apobates.jforum2.troll.regular.event.BoardActionLog;
import com.apobates.jforum2.troll.regular.event.PostsActionLog;
import com.apobates.jforum2.troll.regular.event.TopicActionLog;
import com.apobates.jforum2.troll.regular.event.TopicMoveLog;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.function.Consumer;

@Configuration
public class RabbitMQEventActionConfig {
    @Autowired
    private TopicActionCollectionService topicActionCollectionService;
    @Autowired
    private BoardActionCollectionService boardActionCollectionService;
    private final static Logger logger = LoggerFactory.getLogger(RabbitMQEventActionConfig.class);

    //spring-cloud-stream事件消费者
    //话题发布事件不是由AspectJ来生成的
    @Bean
    public Consumer<String> topicAction() {
        logger.error("[TE]start consume topic event");
        return talBody -> {
            TopicActionLog tal = new Gson().fromJson(talBody, TopicActionLog.class);
            logger.error("[MDA][Receive]topic action: "+tal.getAction().getTitle()+", operator: "+tal.getMemberNickname());
            TopicActionCollection tac = new TopicActionCollection(
                    tal.getMemberNickname(),
                    tal.getMemberId(),
                    tal.getAction(),
                    tal.getTopicId(),
                    tal.getIpAddr(),
                    tal.getToken(),
                    tal.getRodeMember());
            tac.setTopicTitle(tal.getTopicTitle());
            //io.seata.rm.datasource.undo.parser.JacksonUndoLogParser.decode(JacksonUndoLogParser.java:170)
            tac.setEntryDateTime(tal.getDateTime());
            topicActionCollectionService.create(tac);
        };
    }

    @Bean
    public Consumer<String> topicMoveAction(){
        logger.error("[TE]start consume topic move event");
        return tmalBody -> {
            TopicMoveLog tmal = new Gson().fromJson(tmalBody, TopicMoveLog.class);

            TopicActionCollection tac = new TopicActionCollection(
                    tmal.getManagerNickname(),
                    tmal.getManager(),
                    ForumActionEnum.TOPIC_MOVE,
                    tmal.getId(),
                    "127.0.0.1",
                    "-",
                    tmal.getManager());
            tac.setTopicTitle(tmal.getTitle());
            //io.seata.rm.datasource.undo.parser.JacksonUndoLogParser.decode(JacksonUndoLogParser.java:170)
            tac.setEntryDateTime(tmal.getDateTime());
            topicActionCollectionService.create(tac);
        };
    }

    @Bean
    public Consumer<String> boardAction() {
        logger.error("[TE]start consume board event");
        return balBody -> {
            BoardActionLog bal = new Gson().fromJson(balBody, BoardActionLog.class);
            logger.error("[MDA][Receive]board action: "+bal.getAction().getTitle()+", operator: "+bal.getMemberNickname());
            BoardActionCollection bac = new BoardActionCollection(
                    bal.getMemberNickname(),
                    bal.getMemberId(),
                    bal.getAction(),
                    bal.getBoardId(),
                    bal.getIpAddr(),
                    bal.getToken(),
                    bal.getRodeMember());
            bac.setBoardTitle(bal.getBoardTitle());
            //io.seata.rm.datasource.undo.parser.JacksonUndoLogParser.decode(JacksonUndoLogParser.java:170)
            bac.setEntryDateTime(bal.getDateTime());
            boardActionCollectionService.create(bac);
        };
    }

    @Bean
    public Consumer<String> postsAction(){
        logger.error("[TE]start consume posts event");
        return palBody -> {
            PostsActionLog pal = new Gson().fromJson(palBody, PostsActionLog.class);
            logger.error("[MDA][Receive]posts action: "+pal.getAction().getTitle()+", operator: "+pal.getMemberNickname());
            TopicActionCollection pac = new TopicActionCollection(
                    pal.getMemberNickname(),
                    pal.getMemberId(),
                    pal.getAction(),
                    pal.getTopicId(),
                    pal.getPostsId(),
                    pal.getIpAddr(),
                    pal.getToken(),
                    pal.getRodeMember());
            //pac.setTopicTitle(pal.getTopicTitle());
            //io.seata.rm.datasource.undo.parser.JacksonUndoLogParser.decode(JacksonUndoLogParser.java:170)
            pac.setEntryDateTime(pal.getDateTime());
            topicActionCollectionService.create(pac);
        };
    }
}
